package com.living.player.dialog;

import android.annotation.SuppressLint;
import android.app.Activity;
import android.app.Dialog;
import android.content.Context;
import android.content.Intent;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.alibaba.android.arouter.launcher.ARouter;
import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.listener.OnItemChildClickListener;
import com.chad.library.adapter.base.listener.OnItemClickListener;
import com.living.Constant;
import com.living.R;
import com.living.filedownloader.bean.TasksManagerModel;
import com.living.filedownloader.ui.audiosel.TasksSelectActivity;
import com.living.filedownloader.ui.batch.DownloadBatchActivity;
import com.living.player.db.AudioPlayListManger;
import com.living.player.services.MediaPlayerServiceImp;
import com.md.basedpc.persistence.PHSPUtil;
import com.md.livingwidget.dialog.PHDialogBackLinstener;
import com.md.livingwidget.dialog.PHDialogBackLinstenerImpl;
import com.md.livingwidget.dialog.PHDialogUtils;
import com.md.livingwidget.emptyview.PHEmptyViewUI;
import com.md.network.api.Audio;

import java.util.ArrayList;
import java.util.List;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

/**
 * 自动播放列表
 */
public class RecentPlayListDialog extends Dialog implements OnItemChildClickListener, OnItemClickListener, View.OnClickListener {

    private Context mContext;
    private RelativeLayout rootSummary;
    private ImageView iv_recent_play_order, iv_recent_play_add, iv_recent_play_download, iv_recent_play_delete;
    private TextView tv_recent_play_order_text, ivClose;
    private RecyclerView recycler_recent_list;
    private RecentPlayListAdapter recentPlayListAdapter;

    public RecentPlayListDialog(Context context) {
        super(context, R.style.custom_dialog2);
        setContentView(R.layout.dialog_player_recent_list);
        this.mContext = context;
        initDialog();
    }

    public void refresh() {
        if (recentPlayListAdapter != null) {
            recentPlayListAdapter.notifyDataSetChanged();
        }
    }

    /***
     * 显示dialog
     */
    public void showDialog() {
        if (!isShowing()) {
            List<Audio> audioList = AudioPlayListManger.getInstance().getAllRecentAudios();
            recentPlayListAdapter.setNewInstance(audioList);
            refreshOrder();
            show();
        }
    }

    void refreshOrder() {
        int order = PHSPUtil.getInstance().getInt(Constant.player_order, 0);
        if (order == 0) {
            iv_recent_play_order.setImageResource(R.drawable.icon_play_control_order_all);
            tv_recent_play_order_text.setText("顺序播放(" + recentPlayListAdapter.getData().size() + "首)");
        } else if (order == 1) {
            iv_recent_play_order.setImageResource(R.drawable.icon_play_control_order_loop);
            tv_recent_play_order_text.setText("单曲循环(" + recentPlayListAdapter.getData().size() + "首)");
        } else {
            iv_recent_play_order.setImageResource(R.drawable.icon_play_control_order_radom);
            tv_recent_play_order_text.setText("随机播放(" + recentPlayListAdapter.getData().size() + "首)");
        }
    }

    /**
     * 关闭dialog
     */
    public void dismissDialog() {
        if (isShowing()) {
            dismiss();
        }
    }

    @SuppressLint("NewApi")
    private void initDialog() {
        rootSummary = findViewById(R.id.root_video);
        iv_recent_play_order = findViewById(R.id.iv_recent_play_order);
        iv_recent_play_add = findViewById(R.id.iv_recent_play_add);
        iv_recent_play_download = findViewById(R.id.iv_recent_play_download);
        iv_recent_play_delete = findViewById(R.id.iv_recent_play_delete);
        tv_recent_play_order_text = findViewById(R.id.tv_recent_play_order_text);
        recycler_recent_list = findViewById(R.id.recycler_recent_list);
        ivClose = findViewById(R.id.tv_recent_play_close);
        recycler_recent_list.setLayoutManager(new LinearLayoutManager(getContext()));
        findViewById(R.id.layout_play_order).setOnClickListener(this);
        ivClose.setOnClickListener(this);

        findViewById(R.id.root_video).setOnClickListener(this);
        iv_recent_play_add.setOnClickListener(this);
        iv_recent_play_download.setOnClickListener(this);
        iv_recent_play_delete.setOnClickListener(this);

        Window window = getWindow();
        if (window != null) {
            window.setWindowAnimations(R.style.dialogWindowAnim);
            WindowManager.LayoutParams params = window.getAttributes();
            params.width = WindowManager.LayoutParams.MATCH_PARENT;
            params.height = WindowManager.LayoutParams.MATCH_PARENT;
            window.setAttributes(params);
        }

        recentPlayListAdapter = new RecentPlayListAdapter();
        recycler_recent_list.setAdapter(recentPlayListAdapter);

        PHEmptyViewUI phEmptyViewUI = new PHEmptyViewUI(getContext());
        phEmptyViewUI.setEmptyText("没有播放的歌曲");
        recentPlayListAdapter.setEmptyView(phEmptyViewUI);

        recentPlayListAdapter.setOnItemClickListener(this);
        recentPlayListAdapter.setOnItemChildClickListener(this);
    }

    @Override
    public void onClick(View v) {
        int _id = v.getId();
        if (_id == R.id.tv_recent_play_close || _id == R.id.root_video) {
            dismissDialog();
        } else if (_id == R.id.layout_play_order) {
            int order = PHSPUtil.getInstance().getInt(Constant.player_order, 0);
            if (order == 0) {
                PHSPUtil.getInstance().putInt(Constant.player_order, 1);
            } else if (order == 1) {
                PHSPUtil.getInstance().putInt(Constant.player_order, 2);
            } else {
                PHSPUtil.getInstance().putInt(Constant.player_order, 0);
            }
            refreshOrder();
        } else if (_id == R.id.iv_recent_play_add) {
            List<Audio> datas = recentPlayListAdapter.getData();
            if (datas != null && datas.size() > 0) {
                ArrayList<Audio> audios = new ArrayList<>();
                audios.addAll(datas);
                ARouter.getInstance().build("/album/selectaudios").withParcelableArrayList("SELECT_AUDIOS", audios).navigation();
            }

        } else if (_id == R.id.iv_recent_play_download) {
            ArrayList<Audio> datas = (ArrayList<Audio>) recentPlayListAdapter.getData();
            TasksSelectActivity.intentTo((Activity) mContext, datas);
        } else if (_id == R.id.iv_recent_play_delete) {
            PHDialogUtils.showTwo(mContext, "确定清空所有歌曲", "提示", new PHDialogBackLinstenerImpl() {
                @Override
                public void rightBtn() {
                    AudioPlayListManger.getInstance().deleteAllAudio();
                    dismissDialog();
                }
            });
        }
    }

    @Override
    public void onItemChildClick(@NonNull BaseQuickAdapter adapter, @NonNull View view, int position) {
        if (view.getId() == R.id.iv_player_recent_delete) {
            Audio audio = recentPlayListAdapter.getData().get(position);
//            Toast.makeText(getContext(), "iv_player_recent_delete ", Toast.LENGTH_LONG).show();
            AudioPlayListManger.getInstance().deleteAudio(audio.getId());
            recentPlayListAdapter.removeAt(position);
        }
    }

    @Override
    public void onItemClick(@NonNull BaseQuickAdapter<?, ?> adapter, @NonNull View view, int position) {
        Audio audio = recentPlayListAdapter.getData().get(position);
//        VideoActivity.intentTo(getContext(), audio);
        MediaPlayerServiceImp.playAudio(getContext(), audio);
        recentPlayListAdapter.notifyDataSetChanged();
    }

}
